//
//  SpecialDetailImagetableViewCell.m
//  BTRIP
//
//  Created by dllo on 15/11/22.
//  Copyright © 2015年 johnbk. All rights reserved.
//

#import "SpecialDetailImagetableViewCell.h"
#import <SDWebImageManager.h>
#import <UIImageView+WebCache.h>
#import "SpecialUserModel.h"
@implementation SpecialDetailImagetableViewCell

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

-(void)dealloc{
    [_picImgView release];
    [_secondModel release];
    [_labelOfDesc release];
//    [_buttonOfAttraction_name release];
    [super dealloc];
}
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        [self createView];
    }
    return self;
}
- (void)createView{
    self.picImgView = [[UIImageView alloc]init];
    [self.contentView addSubview:self.picImgView];
    [_picImgView release];
    
//    self.button = [UIButton buttonWithType:UIButtonTypeCustom];
//    self.button.alpha = 0;
//    [self.contentView addSubview:self.button];
//    [self.button release];
    
    self.labelOfDesc = [[UILabel alloc]init];
    [self.contentView addSubview:self.labelOfDesc];
    [_labelOfDesc release];
    self.labelOfDesc.font = [UIFont systemFontOfSize:14];
    //self.labelOfDesc.backgroundColor = [UIColor cyanColor];
    self.labelOfDesc.numberOfLines = 0;
    //换行模式
    self.labelOfDesc.lineBreakMode = NSLineBreakByWordWrapping;

}
- (void)layoutSubviews{
    [super layoutSubviews];
     self.picImgView.frame = CGRectMake(10 * SWIDTH, 10 * SWIDTH, KWIDTH - 20, self.heightOfImage);
    
    //    self.button.frame = self.picImgView.frame;
     self.labelOfDesc.frame = CGRectMake(self.picImgView.frame.origin.x, self.picImgView.frame.origin.y + 10 + self.picImgView.frame.size.height, self.picImgView.frame.size.width, self.heightOfDesc);
}

- (void)setSecondModel:(FirstSpecialSecondModel *)secondModel{
    if (_secondModel != secondModel) {
        [_secondModel release];
        _secondModel = [secondModel retain];
    }
    
    [self.picImgView sd_setImageWithURL:[NSURL URLWithString:secondModel.image_url] placeholderImage:[UIImage imageNamed:@"background"]];
    
    self.heightOfImage = (KWIDTH - 40) * (secondModel.image_height.integerValue) / (secondModel.image_width.integerValue);
    
//    self.labelOfDesc.text = [NSString stringWithFormat:@"%@",secondModel.textDescription];
    
   self.heightOfDesc = [SuitableHeight suitableHeight:secondModel.textDescription andWidth:self.labelOfDesc.frame.size.width andFont:14];
   
    //将userId 替换成姓名
    NSArray *array = [secondModel.textDescription componentsSeparatedByString:@"@"];
   NSMutableString *handledDesc = [NSMutableString string];

    if ([array[0] hasPrefix:@"#"]) {
        for (int i = 1; i < array.count; i++) {
            for (SpecialUserModel *user in secondModel.user_idsArray) {
                if ([array[i] containsString:user.number]) {
                    NSMutableString *mutableString = [NSMutableString stringWithString:array[i]];
                    NSRange range = [array[i] rangeOfString:user.number];
                    
                    [mutableString replaceCharactersInRange:range withString:user.name];
                    [handledDesc appendString:[NSMutableString stringWithFormat:@"@%@", mutableString]];
                }
            }
            
        }
        
        NSString *stringOfDesc = [NSString stringWithFormat:@"%@%@", array[0], handledDesc];
        NSArray *arrayTwo = [stringOfDesc componentsSeparatedByString:@"@"];
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:stringOfDesc];
        
        for (int i = 1; i < arrayTwo.count; i++) {
            for (SpecialUserModel *user in secondModel.user_idsArray) {
                if ([arrayTwo[i] containsString:user.name]) {
                    [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSRangeFromString(user.name)];
                }
            }
            
            
        }
        
        
        [self.labelOfDesc setAttributedText:attributedString];
        
    }else if ([array[0] hasPrefix:@"@"]){
        for (int i = 0; i < array.count; i++) {
            for (SpecialUserModel *user in secondModel.user_idsArray) {
                if ([array[i] containsString:user.number]) {
                    NSMutableString *mutableString = [NSMutableString stringWithString:array[i]];
                    NSRange range = [array[i] rangeOfString:user.number];
                    
                    [mutableString replaceCharactersInRange:range withString:user.name];
                    [handledDesc appendString:[NSMutableString stringWithFormat:@"@%@", mutableString]];
                }
            }
        }
        
        NSString *stringOfDesc = [NSString stringWithFormat:@"%@%@", array[0], handledDesc];
        NSArray *arrayTwo = [stringOfDesc componentsSeparatedByString:@"@"];
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc]initWithString:stringOfDesc];
        
        for (int i = 1; i < arrayTwo.count; i++) {
            for (SpecialUserModel *user in secondModel.user_idsArray) {
                if ([arrayTwo[i] containsString:user.name]) {
                    [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor blueColor] range:NSRangeFromString(user.name)];
                }
            }
        }
    }else{
        self.labelOfDesc.text = secondModel.textDescription;
    }
    
    [self layoutSubviews];
//
}

    
    
    
    
   





@end
